class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price


class Member:
    def __init__(self, level="普通会员"):
        self.level = level

    @property
    def discount_rate(self):
        """根据会员等级返回折扣率"""
        rates = {
            "普通会员": 1.0,  # 无折扣
            "银卡会员": 0.9,  # 9折
            "金卡会员": 0.8,  # 8折
            "钻石会员": 0.7  # 7折
        }
        return rates.get(self.level, 1.0)


class CartItem:
    def __init__(self, product, qty, member=None):
        self.product = product
        self.qty = qty
        self.member = member if member else Member()  # 默认普通会员

    def subtotal(self):
        """计算折前小计"""
        return self.product.price * self.qty

    def discounted_subtotal(self):
        """计算折后小计"""
        return self.subtotal() * self.member.discount_rate


class ShoppingCart:
    def __init__(self, member=None):
        self.items = []
        self.member = member if member else Member()  # 默认普通会员

    def add(self, product, qty=1):
        """添加商品到购物车，使用购物车的会员等级"""
        self.items.append(CartItem(product, qty, self.member))

    def total(self):
        """计算折前总价"""
        return sum(item.subtotal() for item in self.items)

    def discounted_total(self):
        """计算折后总价"""
        return sum(item.discounted_subtotal() for item in self.items)

    def discount_amount(self):
        """计算折扣金额"""
        return self.total() - self.discounted_total()

    def show(self):
        print("-" * 40)
        for item in self.items:
            print(f"{item.product.name} ×{item.qty}\t￥{item.subtotal():.2f}", end="")
            if item.member.discount_rate < 1.0:
                print(f" (折后: ￥{item.discounted_subtotal():.2f}, {int((1 - item.member.discount_rate) * 100)}折)",
                      end="")
            print()
        print("-" * 40)
        print(f"商品总额：￥{self.total():.2f}")
        if self.discount_amount() > 0:
            print(
                f"会员折扣：-￥{self.discount_amount():.2f} ({self.member.level} {int((1 - self.member.discount_rate) * 100)}折)")
        print(f"应付总额：￥{self.discounted_total():.2f}")


# --- 测试 ---
apple = Product("苹果", 3.5)
bread = Product("面包", 4.0)
milk = Product(name="牛奶",price=6.5)
print("=== 普通会员 ===")
cart1 = ShoppingCart()
cart1.add(apple, 4)
cart1.add(bread, 2)
cart1.add(milk,qty=3)
cart1.show()

print("\n=== 金卡会员 ===")
gold_member = Member("金卡会员")
cart2 = ShoppingCart(gold_member)
cart2.add(apple, 4)
cart2.add(bread, 2)
cart2.add(milk,qty=6)
cart2.show()

print("\n=== 钻石会员 ===")
diamond_member = Member("钻石会员")
cart3 = ShoppingCart(diamond_member)
cart3.add(apple, 4)
cart3.add(bread, 2)
cart3.add(milk,qty=8)
cart3.show()